const serviceSchema = require('./schema/serviceSchema')

const serviceModel = {}

serviceModel.addService =  (service) => {
  const data = new serviceSchema({
    name: service.name,
    contact: service.contact,
    address: service.address,
    yixiang: service.yixiang,
    problem: service.problem,
    ip: service.ip
  })
  return new Promise((resolve, reject) => {
    data.save((err, res) => {
      if(err) {
        reject(err)
      } else {
        resolve(res)
      }
    })
  })
}

serviceModel.getServiceList = (num, total) => {
  const skip = (num - 1) * total
  return new Promise((resolve, reject) => {
    serviceSchema.find().skip(skip).limit(Number(total)).exec((err, rows) => {
      if (err) {
        reject(err)
      } else {
        resolve(rows)
      }
    })
  })
}

serviceModel.getServiceCount = () => {
  return new Promise((resolve, reject) => {
    serviceSchema.countDocuments().exec((err, count) => {
      if (err) {
        reject(err)
      } else {
        resolve(count)
      }
    })
  })
}

module.exports = serviceModel
